#include "mainwindow.h"
#include "clickablelabel.h"
#include <QDesktopServices>
#include <QUrl>
#include <QString>
#include <QIcon>
#include <QThread>
#include <QObject>

#include "backupworker.h"
#include "fileUtil.hpp"
#include "dataManager.hpp"
#include "clientBackup.hpp"
#include "backup_client.h"

extern DatabaseManager dbManager;
MainWindow::MainWindow(QWidget *parent, int usrID)
    : QWidget(parent)
    , _usrID(usrID)
{
    qDebug() << "MainWindow created with user ID:" << _usrID;
    QString usrName;
    dbManager.getUsrNameByUsrID(_usrID, usrName);
    QString winTitle = "云备份系统 Welcome " + usrName + " ！";
    setWindowTitle(winTitle);
    setFixedSize(800, 600);
    QIcon icon(":/cloudIcon.png");
    setWindowIcon(icon);

    // 布局
    QVBoxLayout *layout = new QVBoxLayout(this);

    // 文件列表框
    fileList = new QListWidget(this);
    layout->addWidget(fileList);

    // 打开文件夹按钮
    QPushButton *openFolderButton = new QPushButton("选择备份文件夹", this);
    openFolderButton->setMaximumHeight(50);
    openFolderButton->setMaximumWidth(300);
    connect(openFolderButton, &QPushButton::clicked, this, &MainWindow::openFolder);

    //刷新文件列表按钮
    QPushButton* refreshListBtn = new QPushButton(this);
    refreshListBtn->setToolTip("刷新文件列表");
    QIcon iconRefresh(":/refresh.png");
    refreshListBtn->setIcon(iconRefresh);
    refreshListBtn->setMaximumHeight(50);
    refreshListBtn->setMaximumWidth(50);
    QHBoxLayout *hlayout = new QHBoxLayout;
    connect(refreshListBtn, &QPushButton::clicked, this, &MainWindow::refreshListMsg);
    hlayout->addWidget(openFolderButton);
    layout->addSpacerItem(new QSpacerItem(50, 50, QSizePolicy::Expanding, QSizePolicy::Minimum));
    hlayout->addWidget(refreshListBtn);
    layout->addLayout(hlayout);

    // 使用 ClickableLabel 替代 QLabel
    ClickableLabel *linkLabel = new ClickableLabel(QString("点击访问云端文件"), this);
    QString labelQss("color: blue; text-decoration: underline; font: 75 16pt \"Consolas\"; min-width: 100px;min-height: 50px;");
    linkLabel->setStyleSheet(labelQss);
    linkLabel->setCursor(Qt::PointingHandCursor); // 光标变为手型
    // 当左键点击linkLabel 触发ClickableLabel::clicked 继而由labelClicked来处理
    connect(linkLabel, &ClickableLabel::clicked, this, &MainWindow::labelClicked); // 连接 clicked 信号
    layout->addWidget(linkLabel);
}

void MainWindow::openFolder()
{
    currentDirPath = QFileDialog::getExistingDirectory(this, "选择备份文件夹");
    refreshListMsg();

    client_backup = new cloudBackup::Backup_Client(nullptr, currentDirPath.toStdString(), _usrID);
}

void MainWindow::labelClicked()
{
    // 服务端处理showFile请求必须识别到usrID和密码 只显示该用户的文件
    QString passwd ;
    dbManager.getPasswdByUsrID(_usrID, passwd);
    QString cloudUrl = "http://120.46.25.211:8585/showfile/" + QString::number(_usrID) + "/" + passwd;
    QDesktopServices::openUrl(QUrl(cloudUrl));
}

void MainWindow::refreshListMsg()
{
    if (!currentDirPath.isEmpty())
    {
        QDir dir(currentDirPath);
        QStringList files = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
        fileList->clear(); // 清空之前的文件列表
        fileList->addItems(files); // 更新文件列表
    }
}


